import { Component, OnInit } from '@angular/core';
import { NavController } from '@ionic/angular';
import { ActivatedRoute, Params } from '@angular/router';
import { ToastService } from 'src/app/providers/toast/toast.service';
import * as constants from '../../../providers/Constants'
import { OrderService } from 'src/app/services/order.service';
import { Storage } from 'src/app/providers/Storage';
import { AuthService } from 'src/app/services/auth.service';
@Component({
  selector: 'app-discount',
  templateUrl: './discount.page.html',
  styleUrls: ['./discount.page.scss'],
})
export class DiscountPage implements OnInit {
  isPitch: boolean = false;
  discountArray: any = [];
  constructor(private nav: NavController, private order: OrderService, private activeRoute: ActivatedRoute, private auth: AuthService, private toast: ToastService) {
    this.activeRoute.queryParams.subscribe((params: Params) => {
      if (params.type != undefined) {
        if (params.type == 1) {
          this.isPitch = true;
        }
      }
    })
  }
  use(item) {
    if (this.isPitch && item.couponStatus == 0) {
      console.log('***************',item.couponStatus);
      Storage.sessionStorage.set('couponAccInfo',item);
      this.nav.pop();
      return
    } else {
      if (item.couponStatus == 0) {
        this.nav.navigateForward('/tabs/tab2');
        console.log('去使用');
      }else{
        console.log('couponAccInfo---------',item);
        // Storage.localStorage.set('couponAccInfo',item);
        // this.nav.pop();
      }
    }
  }
  noUse(){
    Storage.sessionStorage.remove('couponAccInfo');
    this.nav.pop();
  }
  AmountOfConversion(num: number) {
    return num / 100;
  }
  AmountOfConversion1(num: number) {
    return num / 100+0.01;
  }
  ngOnInit() {
    this.auth.getUserSelCoupon({
    }).subscribe(res => {
      // 保存成功返沪上一页，否则弹出提示框
      if (res.retCode == '000000') {
        this.discountArray = res.data;
      } else if (constants.errCode[res.retCode]) {
        if (constants.errCode[res.retCode] != '0') {
          this.toast.show(constants.errCode[res.retCode]);
        }
      } else if (res.retMsg) {
        this.toast.show(res.retMsg);
      } else {
        this.toast.show("exception service routine");
      }
    });


  }



  discountStr(type) {
    if (type == 0) {
      return 'use';
    } else if (type == 1) {
      return 'Have already used';
    } else {
      return 'stale dated';
    }
  }

}
